package com.cyz.web.app.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

@RequestMapping("strategy")
@RestController
public class ClientController {


    @Autowired
    private List<LoginService> loginServices;

    @GetMapping("login")
    public String login(String type, String userName, String password) {

        AtomicReference<String> userInfo = new AtomicReference<>(null);
        loginServices.stream().forEach(x -> {
            if (x.getCurrentName().equals(type)) {
                userInfo.set(x.login(userName, password));
            }
        });

        return Optional.ofNullable(userInfo.get()).orElseThrow(()->new RuntimeException("登录失败"));
    }

}
